<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/9/30  10:17
 */
namespace app\admin\controller;
use think\Db;
class Article extends Common
{
    private $table_admin = '';

    /*
	*	构造函数
	*/
    function __construct()
    {
        parent::__construct();
        $this->table_admin = db('article');
    }

    //文章列表
    public function lists()
    {
        $cid = input('id');
        $this->assign('cid', $cid);
        $list = $this->table_admin->where(['cid'=>$cid])->order('id desc')->paginate();
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    //添加
    public function add()
    {
        $cid = input('cid');
        $this->assign('cid', $cid);
        return $this->fetch('article/edit');
    }
    //编辑文章
    public function edit()
    {
        if(request()->isAjax()){
            $id = input('id');
            $cid = input('cid');
            $title = input('title');
            $content = input('content');
            $status = input('status');
            $sort = input('sort');
            $introduction = input('introduction');
            $data['title'] = $title;
            $data['content'] = $content;
            $data['status'] = $status;
            $data['sort'] = $sort;
            $data['introduction'] = $introduction;
            $file = request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public/static/uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['image'] =  $info->getSaveName();
                    if(!empty($id)){
                        $oldimg = db('article')->where(['id'=>$id])->value('image');
                        $oldimgurl = ROOT_PATH . 'public/static/uploads/'.$oldimg;
                        // if(file_exists($oldimgurl)){
                        //     unlink($oldimgurl);
                        // }
                    }
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            if(empty($id)){
                //添加
                $data['cid'] = $cid;
                $data['create_time'] = time();
                $bool = $this->table_admin->insert($data);
            }else{
                //修改
                $bool = $this->table_admin->where(['id'=>$id])->update($data);
            }
            if(!$cid){
                $cid = $this->table_admin->where(['id'=>$id])->value('cid');
            }
            if(empty($bool))$this->error('操作失败！');
            $this->success('操作成功！', url('Article/lists',array('id'=>$cid)));
        }

        $id = input('id');
        if(!empty($id)){
            $da = $this->table_admin->where(['id'=>$id])->find();
            $this->assign('da', $da);
        }else{
            $cid = input('cid');
            $da = $this->table_admin->where(['cid'=>$cid])->find();
            $this->assign('da', $da);
        }
        return $this->fetch();
    }

    //删除
    public function del()
    {
        $id = input('id');
        if(!$id)$this->error('参数有误');
        $data = $this->table_admin->where(['id'=>$id])->find();
        if(!$data)$this->error('数据不存在');
        $img = ROOT_PATH . 'public/static/uploads/'.$data['image'];
        if($data['image'] || is_file($img)){
            unlink($img);
        }
        $res = $this->table_admin->where(['id'=>$id])->delete();
        if($res)$this->success('操作成功！');
        $this->error('操作失败！');
    }
}
